//
//  WPChangePayPwdViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/4.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPChangePayPwdViewController.h"
#import "WPChangePayPwdViewModel.h"

@interface WPChangePayPwdViewController ()
@property (nonatomic, strong) UILabel * bfPwdLabel;/**< 旧密码说明 */
@property (nonatomic, strong) UITextField * bfPwdTextField;/**< 旧密码输入框 */
@property (nonatomic, strong) UILabel * pwdLabel;/**< 新密码说明 */
@property (nonatomic, strong) UITextField * pwdTextField;/**< 新密码输入框 */
@property (nonatomic, strong) UIButton * changePwdBtn;/**< 确认修改 */
@property (nonatomic, strong) UITextField * codeTextField;/**< 验证码输入框 */
@property (nonatomic, strong) UIButton * getCodeBtn;/**< 获取验证码按钮 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 时间记录 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation WPChangePayPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init 
- (void)initializeUserInterface {
    self.title = @"修改支付密码";
    [self.view addSubview:self.bfPwdLabel];
    [self.view addSubview:self.bfPwdTextField];
    [self.view addSubview:self.pwdLabel];
    [self.view addSubview:self.pwdTextField];
    [self.view addSubview:self.changePwdBtn];
}
#pragma mark - responds events
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == _bfPwdTextField && textField.text.length > 14) {
        textField.text = [textField.text substringFromIndex:14];
    }
    if (textField == _pwdTextField && textField.text.length > 14) {
        textField.text = [textField.text substringToIndex:14];
    }
}
- (void)respondsToChangePwdBtn {
    [self.view endEditing:YES];
    if (_bfPwdTextField.text.length < 6 || _pwdTextField.text.length < 6) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入至少6位数密码"];
        return;
    }
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.color = [UIColor lightGrayColor];
    hud.activityIndicatorColor = [UIColor blackColor];
    hud.labelText = @"请稍候";
    hud.detailsLabelText = @"修改密码中...";
    __weak typeof(self)weakself = self;
    [WPChangePayPwdViewModel changePayPwdWithBfPaypwd:_bfPwdTextField.text andWithNewPwd:_pwdTextField.text andWithSuccessBlock:^(NSString *succ) {
        hud.labelText = succ;
        hud.detailsLabelText = nil;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud hide:YES];
            [weakself.navigationController popViewControllerAnimated:YES];
        });
    } andWithFailureBlock:^(NSString *failure) {
        hud.labelText = failure;
        hud.detailsLabelText = @"请稍候再试";
        [hud hide:YES afterDelay:2.0];
    }];
}
#pragma mark - getter
- (UILabel *)bfPwdLabel {
    if (!_bfPwdLabel) {
        _bfPwdLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), navBar_Fheight+5*DHFlexibleHorizontalMutiplier(), 300, 35*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"旧密码：";
            lab;
        });
    }
    return _bfPwdLabel;
}
- (UITextField *)bfPwdTextField {
    if (!_bfPwdTextField) {
        _bfPwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bfPwdLabel.frame), SCREEN_SIZE.width, 40*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10*DHFlexibleHorizontalMutiplier(), 45*DHFlexibleHorizontalMutiplier())];
            text.leftView = view;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.keyboardType = UIKeyboardTypeNumberPad;
            text.returnKeyType = UIReturnKeyNext;
            text.secureTextEntry = YES;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.backgroundColor = [UIColor whiteColor];
            text.placeholder = @"请输入您当前支付密码";
            text;
        });
    }
    return _bfPwdTextField;
}
- (UILabel *)pwdLabel {
    if (!_pwdLabel) {
        _pwdLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.bfPwdTextField.frame), 300, 45*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"新密码：";
            lab;
        });
    }
    return _pwdLabel;
}
- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pwdLabel.frame), SCREEN_SIZE.width, 45*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10*DHFlexibleHorizontalMutiplier(), 45*DHFlexibleHorizontalMutiplier())];
            text.leftView = view;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.keyboardType = UIKeyboardTypeNumberPad;
            text.returnKeyType = UIReturnKeyNext;
            text.secureTextEntry = YES;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.backgroundColor = [UIColor whiteColor];
            text.placeholder = @"请输入至少6位数密码";
            text;
        });
    }
    return _pwdTextField;
}
- (UIButton *)changePwdBtn {
    if (!_changePwdBtn) {
        _changePwdBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame) + 40*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"确认修改" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToChangePwdBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _changePwdBtn;
}
- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.bfPwdTextField.frame), 220*DHFlexibleHorizontalMutiplier(), CGRectGetHeight(self.preferredFocusedView.bounds))];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.text = @"验证码";
            label.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.leftView = label;
            text.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.keyboardType = UIKeyboardTypeNumberPad;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.placeholder = @"请输入短信验证码";
            text;
        });
    }
    return _codeTextField;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(240*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.codeTextField.frame)+5*DHFlexibleHorizontalMutiplier(), 70*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(225, 225, 225, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _getCodeBtn;
}
- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];
    }
    return _timer;
}

@end
